<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\models\Zdb;
use think\facade\Db;
use app\models\Clb;
use app\models\Xlb;
use app\models\Cpb;
use app\models\Yhb;
use app\api\validate\Destination;

class Serve
{   //车辆信息查询,根据车主姓名，显示报班卡号,车牌照号,线路编号等，再根据线路编号查询线路名称
    public function serve1($czxm='张三'){
        $serve1=Clb::alias('a')
            ->where('czxm',$czxm)
            ->join('U_xlb b','a.xlbh = b.xlbh')
            ->field(
                ['a.czxm','a.bbkh','a.cpzh','a.xlbh','b.xlmc','a.ssgs','a.zwgs','a.clzt']  //车主姓名,报班卡号,车牌照号,线路编号,所属公司,座位数,车辆状态
            )
            ->select();
        return json($serve1);
    }

    //根据报班卡号，显示车主姓名,报班卡号,车牌照号,线路编号等，再根据线路编号查询线路名称
    public function serve2($bbkh='15'){
        $serve2=Clb::alias('a')
            ->where('bbkh',$bbkh)
            ->join('U_xlb b','a.xlbh = b.xlbh')
            ->field(
                //车主姓名,报班卡号,车牌照号,线路编号,所属公司,座位数,车辆状态
                ['a.czxm','a.bbkh','a.cpzh','a.xlbh','b.xlmc','a.ssgs','a.zwgs','a.clzt']
            )
            ->select();
        return json($serve2);
    }
    //根据所属公司查询，显示车主姓名,报班卡号,车牌照号,线路编号等，再根据线路编号查询线路名称
    public function serve3($ssgs='邯聊公司'){
        $serve3=Clb::alias('a')
            ->where('ssgs',$ssgs)
            ->join('U_xlb b','a.xlbh = b.xlbh')
            ->field(
                ['a.czxm','a.bbkh','a.cpzh','a.xlbh','b.xlmc','a.ssgs','a.zwgs','a.clzt']  //车主姓名,报班卡号,车牌照号,线路编号,所属公司,座位数,车辆状态
            )
            ->select();
        return json($serve3);
    }
    //根据车牌照号查询，显示车主姓名,报班卡号,车牌照号,线路编号等，再根据线路编号查询线路名称
    public function serve4($cpzh='冀DG9708'){
        $serve4=Clb::alias('a')
            ->where('cpzh',$cpzh)
            ->join('U_xlb b','a.xlbh = b.xlbh')
            ->field(
                ['a.czxm','a.bbkh','a.cpzh','a.xlbh','b.xlmc','a.ssgs','a.zwgs','a.clzt']  //车主姓名,报班卡号,车牌照号,线路编号,所属公司,座位数,车辆状态
            )
            ->select();
        return json($serve4);
    }

    //根据线路编号查询，显示车主姓名,报班卡号,车牌照号,线路编号等，再根据线路编号查询线路名称
    public function serve5($xlbh='114'){
        $serve5=Xlb::alias('b')
            ->where('xlbh',$xlbh)
            ->join('U_clb a','a.xlbh = b.xlbh')
            ->field(
                ['a.czxm','a.bbkh','a.cpzh','a.xlbh','b.xlmc','a.ssgs','a.zwgs','a.clzt']  //车主姓名,报班卡号,车牌照号,线路编号,所属公司,座位数,车辆状态
            )
            ->select();
        return json($serve5);
    }

    //站点查询
    public function getzd($pybm='asa')
    {
        $sites =Zdb::where('pybm',$pybm)
            ->field(['mdzm','mdzh'])
            ->select();
        return json($sites);
    }

    //计算退票金额并展示
    public function jstpje($cpbh='1'){
        $serve6=Cpb::where('cpbh',$cpbh)
            ->field(['pj','rqsj','bcbh'])
            ->select();
        $pj=$serve6[0];
        $time1=$pj['rqsj'];   //$_POST[''];    //结束时间，发车时间
        $currentTime=date("Y-m-d H:i:s");   //开始时间,当前时间,退票时间。
        $a = floor((strtotime($time1)-strtotime($currentTime))%86400/60);   //相差分钟

            if ($a<0){
                echo"已发车，无法退票";
            }elseif($a>120){
                $jstpje=$pj['pj']-($pj['pj']/10);   //据发车时间超过2小时退票金额，手续费为10%
                return $jstpje;
            }elseif($a>0){
                $jstpje=$pj['pj']-($pj['pj']/5);    //据发车时间不足2小时退票金额，手续费为20%
                return $jstpje;
            }
    }

    //退票操作
//    public function serve7($cpbh='4')
//    {
//        //查询车票信息
//        $serve7=Cpb::where('cpbh',$cpbh)
//        ->field(['cpbh','mdcz','spy','pj','rqsj','bcbh'])
//        ->select();

//        $a=$serve7[0];
//        $list=$this->jstpje();
//        $Time=date("Y-m-d H:i:s");  //获取当前时间

//        if ($a['bcbh']=="流水"){
//            $ticket=[
//                'cpbh' => $a['cpbh'],   'mdcz' => $a['mdcz'],   'spy' => $a['spy'],
//                'tpy' => 'tpy',         'pj' => $a['pj'],       'tpje' => $list,
//                'rqsj' => $a['rqsj'],   'tpsj' => $Time,        'bcbh' => $a['bcbh'],
//                'zwh' => $a['zwh'],     'cplx' => $a['cplx'],   'jpk' => $a['jpk'],
//                'bbkh' => $a['bbkh'],   'spsj' => $a['spsj'],   'xlbh' => $a['xlbh'],
//                'mdzh' => $a['mdzh'],   'ssgs' => $a['ssgs'],   'lkxm' => $a['lkxm'],
//                'sfzh' => $a['sfzh']];

//            $result= Db::table('U_tpb')->insert($ticket);  //将信息写入退票表
//            $del=Cpb::where('cpbh',$cpbh)->delete();  //删除原本车票表中信息
//            return "已退票";
//        }else{
//            $tpzw=Clb::where('bcbh',$a['bcbh'])   //固定退票
//            ->inc('zwgs')->update();
//            $ticket=[
//                'cpbh' => $a['cpbh'],   'mdcz' => $a['mdcz'],   'spy' => $a['spy'],
//                'tpy' => 'tpy',         'pj' => $a['pj'],       'tpje' => $list,
//                'rqsj' => $a['rqsj'],   'tpsj' => $Time,        'bcbh' => $a['bcbh'],
//                'zwh' => $a['zwh'],     'cplx' => $a['cplx'],   'jpk' => $a['jpk'],
//                'bbkh' => $a['bbkh'],   'spsj' => $a['spsj'],   'xlbh' => $a['xlbh'],
//                'mdzh' => $a['mdzh'],   'ssgs' => $a['ssgs'],   'lkxm' => $a['lkxm'],
//                'sfzh' => $a['sfzh'],];
//            $result= Db::table('U_tpb')->insert($ticket);
//            $del=Cpb::where('cpbh',$cpbh)->delete();
//            return "已退票";
//        }
//    }

    //已退票查询
//    public function serve8($cpbh='1')
//    {   $serve8 = Db::table('U_tpb')
//            ->where('cpbh', $cpbh)
//            ->field(
//                ['cpbh', 'mdcz', 'spy','tpy', 'pj','tpje','rqsj','tpsj','bcbh']
//            )->select();
//        return json($serve8);}
//
//    public function tpcx()
//    {   $serve8 = Db::table('U_tpb')
//        ->field(
//            ['cpbh', 'mdcz', 'spy','tpy', 'pj','tpje','rqsj','tpsj','bcbh']
//        )->select();
//        return json($serve8);}

    //换乘查询，通过车票编号查询信息
    public function serve9($cpbh='1')
    {
        $serve9 = Cpb::alias('a')
            ->where('a.cpbh',$cpbh)
            ->join('U_xlb b','a.xlbh = b.xlbh')
            ->field(['a.cpbh','b.xlmc','a.rqsj','a.mdcz','a.pj'])    //车票编号，线路名称，发车时间，目的站名，票价,'a.sfjp'，检票状态,'a.spsj'，售票时间,'a.cplx'，车票类型
            ->select();
        return json($serve9);
    }

    //查询当前可换乘车次
    public function serve10($mdcz='111')
    {
        $dqsj=date("Y-m-d H:i:s");
        $serve10 = Xlb::alias('a')
            ->join('U_clb b','a.xlbh = b.xlbh')
            ->where('a.zdcz',$mdcz)
            ->where('b.fcsj','>',$dqsj)
            ->field(['b.xlbh','a.zdcz','a.tjcz','a.xlmc','']) //差票价
            ->select();
        return json($serve10);
    }

    //修改用户密码
    public function serve($yhbh='1'){
        $serve = Db::table('U_yhb')
            ->where('yhbh',$yhbh)
            ->update(['yhmm' => '789456']);
        return "密码已修改";
    }

}